package com.xiaocai.objview;

import android.opengl.GLES20;
import android.util.Log;

public class LoadingCube {

    private VertexArray vertexArray;

    private final int POSITION_COMPONENT_COUNT = 3;

    private final int COLOR_COMPONENT_COUNT = 4;

    private final int STRIDE = (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) * Constants.BYTES_PER_FLOAT;

    public LoadingCube() {
        float[] vertex = new float[] {
                1, 1, 1,    -1, 1, 1,   -1, -1, 1,      1, 1, 1,   -1, -1, 1,    1, -1, 1, // 前

                1, 1, -1,    -1, 1, -1,   -1, -1, -1,   1, 1, -1,   -1, -1, -1,    1, -1, -1, //后

                -1, 1, 1,    -1, 1, -1,    -1, -1, -1,    -1, 1, 1,   -1, -1, -1,   -1, -1, 1, //左

                1, 1, -1,    1, 1, 1,      1, -1, 1,     1, 1, -1,   1, -1, 1,     1, -1, -1, // 右

                1, 1, -1,   -1, 1, -1,   -1, 1, 1,       1, 1, -1,  -1, 1, 1,    1, 1, 1, //上

                1, -1, -1,   -1, -1, -1,   -1, -1, 1,       1, -1, -1,  -1, -1, 1,    1, -1, 1, //下
        };
        float[] colors = new float[] {
                1, 0, 0, 0.5f,    1, 0, 0, 0.5f,   1, 0, 0, 0.5f,      1, 0, 0, 0.5f,   1, 0, 0, 0.5f,    1, 0, 0, 0.5f,
                1, 1, 0, 0.5f,    1, 1, 0, 0.5f,   1, 1, 0, 0.5f,      1, 1, 0, 0.5f,   1, 1, 0, 0.5f,    1, 1, 0, 0.5f,
                1, 0, 1, 0.5f,    1, 0, 1, 0.5f,   1, 0, 1, 0.5f,      1, 0, 1, 0.5f,   1, 0, 1, 0.5f,    1, 0, 1, 0.5f,
                0, 1, 1, 0.5f,    0, 1, 1, 0.5f,   0, 1, 1, 0.5f,      0, 1, 1, 0.5f,   0, 1, 1, 0.5f,    0, 1, 1, 0.5f,
                0, 0, 1, 0.5f,    0, 0, 1, 0.5f,   0, 0, 1, 0.5f,      0, 0, 1, 0.5f,   0, 0, 1, 0.5f,    0, 0, 1, 0.5f,
                1, 0, 0, 0.5f,    1, 0, 0, 0.5f,   1, 0, 0, 0.5f,      1, 0, 0, 0.5f,   1, 0, 0, 0.5f,    1, 0, 0, 0.5f,
        };

        float[] v = new float[vertex.length + colors.length];

        int count = -1;
        for (int i = 0; i < vertex.length; i++) {
            if (i % POSITION_COMPONENT_COUNT == 0) {
                count++;
            }
            v[i + count * COLOR_COMPONENT_COUNT] = vertex[i];
        }
        count = 0;
        for (int i = 0; i < colors.length; i++) {
            if (i % COLOR_COMPONENT_COUNT == 0) {
                count++;
            }
            v[i + count * POSITION_COMPONENT_COUNT] = colors[i];
        }

        vertexArray = new VertexArray(v);
    }
    public void bindData(LoadingCubeShaderProgram loadingCubeShaderProgram) {

        vertexArray.setVertexAttribPointer(0,
                loadingCubeShaderProgram.getPositionLocation(),
                POSITION_COMPONENT_COUNT,
                STRIDE);

        vertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT,
                loadingCubeShaderProgram.getColorLocation(),
                COLOR_COMPONENT_COUNT,
                STRIDE);
    }

    public void draw() {
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36);
    }
}
